package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.system.domain.BO.PurchaseBO;
import com.battery.system.domain.PurchaseItems;
import com.battery.system.domain.VO.PurchaseItemsVO;
import com.battery.system.domain.VO.PurchaseVO;
import com.battery.system.service.battery.IPurchaseItemsService;
import com.battery.system.service.battery.IPurchaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Purchase;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 采购订单Controller
 *
 * @author battery
 * @date 2021-03-23
 */
@Controller
@RequestMapping("/system/purchase")
public class PurchaseController extends BaseController {
    private String prefix = "system/purchase";

    @Autowired
    private IPurchaseService purchaseService;

    @Autowired
    private IPurchaseItemsService purchaseItemsService;

    @RequiresPermissions("system:purchase:view")
    @GetMapping()
    public String purchase() {
        return prefix + "/purchase";
    }

    /**
     * 查询采购订单列表
     */
    @RequiresPermissions("system:purchase:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PurchaseBO purchaseBO) {
        startPage();
//        List<Purchase> list = purchaseService.selectPurchaseList(purchase);
        List<PurchaseVO> list = purchaseService.selectPurchaseVOList(purchaseBO);
        return getDataTable(list);
    }


    /**
     * 展示采购订单对应商品信息
     *
     * @param purchaseItems
     * @return
     */
    @PostMapping("/itemslist")
    @ResponseBody
    public TableDataInfo itemslist(PurchaseItems purchaseItems) {
        startPage();
        List<PurchaseItemsVO> list = purchaseItemsService.selectPurchaseItemsVOList(purchaseItems);
        return getDataTable(list);
    }

    /**
     * 导出采购订单列表
     */
    @RequiresPermissions("system:purchase:export")
    @Log(title = "采购订单" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Purchase purchase) {
        List<Purchase> list = purchaseService.selectPurchaseList(purchase);
        ExcelUtil<Purchase> util = new ExcelUtil<Purchase>(Purchase.class);
        return util.exportExcel(list, "purchase");
    }

    /**
     * 新增采购订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存采购订单
     */
    @RequiresPermissions("system:purchase:add")
    @Log(title = "采购订单" , businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Purchase purchase) {
        return toAjax(purchaseService.insertPurchase(purchase));
    }

    /**
     * 修改采购订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Purchase purchase = purchaseService.selectPurchaseById(id);
        mmap.put("purchase" , purchase);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购订单
     */
    @RequiresPermissions("system:purchase:edit")
    @Log(title = "采购订单" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Purchase purchase) {
        return toAjax(purchaseService.updatePurchase(purchase));
    }

    /**
     * 删除采购订单
     */
    @RequiresPermissions("system:purchase:remove")
    @Log(title = "采购订单" , businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(purchaseService.deletePurchaseByIds(ids));
    }


    /**
     * 跳转到
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/toDelivery/{id}")
    public String refuseCz(@PathVariable("id") String id, ModelMap mmap) {
        Purchase purchase = purchaseService.selectByPrimaryKey(id);
        mmap.put("purchase" , purchase);
        return prefix + "/delivery";
    }


    @Log(title = "采购订单发货操作" , businessType = BusinessType.OTHER)
    @RequiresPermissions("system:endOrder:delivery")
    @PostMapping("/delivery")
    @ResponseBody
    public AjaxResult delivery(Purchase purchase) {

        if (StringUtils.isBlank(purchase.getId())) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(purchase.getExpressName())) {
            return error("请填写快递商");
        }

        if (StringUtils.isBlank(purchase.getExpressNumber())) {
            return error("请填写快递单号");
        }

        return toAjax(purchaseService.delivery(purchase));
    }
}
